package com.zhang.hadoop.util;

import org.apache.commons.io.FileUtils;
import org.junit.Test;

import java.io.File;
import java.util.Collection;

/**
 * @author ：zhangyufei.zhang
 * @date ：Created in 2021-09-03 14:39
 * @description：
 */
public class EncodingChange {

    @Test
    public void test1() {
        try {
            String srcDirPath = "D:\\5-学习\\2-笔记\\设计模式\\代码\\DesignPattern\\src\\com\\zhang.designPattern";
            // 转为UTF-8编码格式源码路径
            String utf8DirPath = "D:\\5-学习\\2-笔记\\设计模式\\代码\\utf-8\\designPattern";

            // 获取所有java文件
            Collection<File> javaGbkFileCol = FileUtils.listFiles(new File(srcDirPath), new String[]{"java"}, true);

            for (File javaGbkFile : javaGbkFileCol) {
                System.out.println(javaGbkFile);
                // UTF8格式文件路径
                String utf8FilePath = utf8DirPath + javaGbkFile.getAbsolutePath().substring(srcDirPath.length());
                // 使用GBK读取数据，然后用UTF-8写入数据
                FileUtils.writeLines(new File(utf8FilePath), "UTF-8", FileUtils.readLines(javaGbkFile, "GBK"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
